from bsccommonlib.csv_persister import CsvPersister
from bsccommonlib.pycommons import abstract
from targetmonitoring.target_models import TargetObject

class AbstractTargetManagementClient(object):
    def __init__(self, csv_file):
        self.exit = False
        self.persister = CsvPersister(csv_file)
        self.selection_list ={
                 1: "Hedefleri listele"
                ,2: "Hedef ekle"
                ,3: "Hedef sil"
                ,0: "Uygulamadan cik"
            }
    

    @abstract 
    def _on_display_requested(self, selection_list):
        pass
    
    @abstract
    def _on_target_list_display_requested(self, targets):
        pass

    @abstract
    def _on_add_target_display_requested(self, target_fields):
        pass
    
    @abstract
    def _on_remove_target_display_requested(self, target_fields):
        pass
    
    @abstract
    def _on_exit(self):
        pass      
    
    def _fire_target_object_to_remove(self,target_id):
        target_object = TargetObject()
        target_object.id = target_id
        self.persister.remove(target_object)
    
    
    def _fire_item_selected(self, selection):
        if selection == 0:
            self.exit = True
            self._on_exit()
        elif selection == 1:
            self._on_target_list_display_requested(self.persister.get_list(TargetObject))
        elif selection == 2:
            self._on_add_target_display_requested(TargetObject)
        elif selection == 3:
            self._on_remove_target_display_requested(TargetObject)

    def _fire_target_ready_to_add(self,target_object):
        if target_object.initial_status < target_object.target_value:
            target_object.increase = 1
            target_object.target_value -= target_object.initial_status
        else:
            target_object.increase = 0
            target_object.target_value = target_object.initial_status - target_object.target_value
            
        self.persister.add(target_object)
        
    def start(self):
        while (self.exit is False):
            self._on_display_requested(self.selection_list)



        
    
                
    
